home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / MAC / MACSCREE.C < prev    next >
C/C++ Source or Header  |  1996-01-02  |  8KB  |  322 lines

  1. #include <QuickDraw.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6.  
  7. #define bufferSize      1024
  8.  
  9. #define screenWindow    128
  10.  
  11. static Rect scrollRect, pauseRect;
  12. static WindowPtr theWindow;
  13. static RgnHandle scrollRgn;
  14.  
  15. static short fontHeight, fontWidth, screenHeight, screenWidth;
  16. static short currentPosition, maxPosition, pausePosition;
  17.  
  18. static short *screenLength, startLine, endLine;
  19. static char *screenImage, **screenLine;
  20.  
  21. static int screenOptions;
  22.  
  23. #define pauseOption     0x0001
  24. #define scrollOption    0x0002
  25.  
  26. void screenOpen(char *Title) {
  27.     FontInfo fontInfo;
  28.     int n;
  29.  
  30.     theWindow = GetNewWindow(screenWindow, nil, (WindowPtr)(-1));
  31.  
  32.     if ((Title != NULL) && (*Title != '\0')) {
  33.         c2pstr(Title);
  34.         SetWTitle(theWindow, (StringPtr)Title);
  35.         p2cstr(Title);
  36.     }
  37.  
  38.     ShowWindow(theWindow);
  39.  
  40.     SetPort(theWindow);
  41.     TextFont(monaco);
  42.     TextSize(9);
  43.  
  44.     GetFontInfo(&fontInfo);
  45.     fontHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  46.     fontWidth = fontInfo.widMax;
  47.  
  48.     scrollRgn = NewRgn();
  49.  
  50.     screenWidth = (theWindow->portRect.right - theWindow->portRect.left - 10) /
  51.         fontWidth;
  52.     screenHeight = (theWindow->portRect.bottom - theWindow->portRect.top) /
  53.         fontHeight;
  54.     maxPosition = screenHeight * fontHeight;
  55.     pausePosition = maxPosition - (currentPosition = fontHeight);
  56.  
  57.     SetRect(&scrollRect, theWindow->portRect.left, theWindow->portRect.top + fontInfo.descent,
  58.         theWindow->portRect.right, theWindow->portRect.bottom);
  59.     SetRect(&pauseRect, theWindow->portRect.left, pausePosition + fontInfo.descent,
  60.         theWindow->portRect.right, theWindow->portRect.bottom);
  61.  
  62.     MoveTo(5, currentPosition);
  63.  
  64.     n = (sizeof(char *) + sizeof(short) + screenWidth) * screenHeight;
  65.  
  66.     screenLine = (char **)NewPtr(n);
  67.  
  68.     screenLength = (short *)&screenLine[screenHeight];
  69.     screenImage = (char *)&screenLength[screenHeight];
  70.  
  71.     for (n = 0; n < screenHeight; n++) {
  72.         screenLine[n] = &screenImage[n * screenWidth];
  73.         screenLength[n] = 0;
  74.     }
  75.  
  76.     startLine = endLine = 0;
  77.  
  78.     screenOptions = 0;
  79.  
  80.     return;
  81. }
  82.  
  83. void screenControl(options, setting) char *options; int setting; {
  84.     int n = 0;
  85.  
  86.     while (*options) {
  87.         switch (*options) {
  88.         case 'p':
  89.             n |= pauseOption;
  90.             break;
  91.         case 's':
  92.             n |= scrollOption;
  93.             break;
  94.         default:
  95.             break;
  96.         }
  97.         options += 1;
  98.     }
  99.  
  100.     if (setting == 0)
  101.         screenOptions &= (n ^ (-1));
  102.     else
  103.         screenOptions |= n;
  104.  
  105.     if ((pausePosition = maxPosition - currentPosition) == 0)
  106.         pausePosition = maxPosition - fontHeight;
  107.  
  108.     return;
  109. }
  110.  
  111. void screenClose(void) {
  112.     DisposPtr((Ptr)screenLine);
  113.  
  114.     DisposeWindow(theWindow);
  115.  
  116.     return;
  117. }
  118.  
  119. void screenUpdate(WindowPtr window) {
  120.     GrafPort *savePort;
  121.     int m, n;
  122.  
  123.     if (window == theWindow) {
  124.         BeginUpdate(window);
  125.         if (!EmptyRgn(window->visRgn)) {
  126.             GetPort(&savePort);
  127.             SetPort(window);
  128.             n = startLine;
  129.             for (m = 1; ; m++) {
  130.                 MoveTo(5, m * fontHeight);
  131.                 if (screenLength[n] != 0)
  132.                     DrawText(screenLine[n], 0, screenLength[n]);
  133.                 if (n == endLine) break;
  134.                 if ((n += 1) == screenHeight) n = 0;
  135.             }
  136.             SetPort(savePort);
  137.         }
  138.         EndUpdate(window);
  139.     }
  140.  
  141.     return;
  142. }
  143.  
  144. static void screenNewline(void) {
  145.     MoveTo(5, currentPosition += fontHeight);
  146.     if (currentPosition > maxPosition) {
  147.         if (screenOptions & scrollOption) {
  148.             ScrollRect(&scrollRect, 0, -fontHeight, scrollRgn);
  149.             MoveTo(5, currentPosition = maxPosition);
  150.             if ((startLine += 1) == screenHeight) startLine = 0;
  151.         } else {
  152.             ScrollRect(&scrollRect, 0, -maxPosition + fontHeight, scrollRgn);
  153.             MoveTo(5, currentPosition = fontHeight + fontHeight);
  154.             startLine = endLine;
  155.         }
  156.     }
  157.     pausePosition -= fontHeight;
  158.  
  159.     if ((endLine += 1) == screenHeight) endLine = 0;
  160.     screenLength[endLine] = 0;
  161.  
  162.     return;
  163. }
  164.  
  165. static char waitChar(void) {
  166.     WindowPtr whichWindow;
  167.     EventRecord theEvent;
  168.  
  169.     for ( ; ; ) {
  170.         SystemTask();
  171.         if (GetNextEvent(everyEvent, &theEvent)) {
  172.             switch (theEvent.what) {
  173.             case keyDown:
  174.                 if ((theEvent.modifiers & cmdKey) &&
  175.                     ((theEvent.message & charCodeMask) == '.'))
  176.                     ExitToShell();
  177.                 return(theEvent.message & charCodeMask);
  178.             case mouseDown:
  179.                 if (FindWindow(theEvent.where, &whichWindow) == inSysWindow)
  180.                     SystemClick(&theEvent, whichWindow);
  181.                 break;
  182.             case updateEvt:
  183.                 screenUpdate((WindowPtr)theEvent.message);
  184.                 break;
  185.             }
  186.         }
  187.     }
  188. }
  189.  
  190. static void screenPause(void) {
  191.     if (pausePosition == 0) {
  192.         if (screenOptions & pauseOption) {
  193.             DrawText("Press any key to continue ...", 0, 29);
  194.             memcpy(screenLine[endLine], "Press any key to continue ...", 29);
  195.             screenLength[endLine] = 29;
  196.  
  197.             (void)waitChar();
  198.  
  199.             EraseRect(&pauseRect);
  200.             MoveTo(5, currentPosition);
  201.             screenLength[endLine] = 0;
  202.         }
  203.  
  204.         pausePosition = maxPosition - fontHeight;
  205.     }
  206.  
  207.     return;
  208. }
  209.  
  210. void screenDisplay(char *s) {
  211.     GrafPort *savePort;
  212.     int m, n;
  213.     char *t;
  214.  
  215.     GetPort(&savePort);
  216.     SetPort(theWindow);
  217.  
  218.     while (*s) {
  219.         screenPause();
  220.  
  221.         for (t = s; (*s) && (*s != '\n') && (*s != '\r'); s++);
  222.  
  223.         if ((n = s - t) > (m = screenWidth - screenLength[endLine])) n = m;
  224.  
  225.         if (n > 0) {
  226.             DrawText(t, 0, n);
  227.             memcpy(screenLine[endLine] + screenLength[endLine], t, n);
  228.             screenLength[endLine] += n;
  229.         }
  230.  
  231.         if ((*s == '\n') || (*s == '\r')) {
  232.             screenNewline();
  233.             s += 1;
  234.         }
  235.     }
  236.  
  237.     SetPort(savePort);
  238.  
  239.     return;
  240. }
  241.  
  242. void screenDump(char *s, long n) {
  243.     GrafPort *savePort;
  244.     int k, m;
  245.     char *t;
  246.  
  247.     GetPort(&savePort);
  248.     SetPort(theWindow);
  249.  
  250.     while (n) {
  251.         screenPause();
  252.  
  253.         for (t = s; (n) && (*s != '\n') && (*s != '\r'); s++, n--);
  254.  
  255.         if ((k = s - t) > (m = screenWidth - screenLength[endLine])) k = m;
  256.  
  257.         if (k > 0) {
  258.             DrawText(t, 0, k);
  259.             memcpy(screenLine[endLine] + screenLength[endLine], t, k);
  260.             screenLength[endLine] += k;
  261.         }
  262.  
  263.         if ((*s == '\n') || (*s == '\r')) {
  264.             screenNewline();
  265.             s += 1;
  266.             n -= 1;
  267.         }
  268.     }
  269.  
  270.     SetPort(savePort);
  271.  
  272.     return;
  273. }
  274.  
  275. char *wfgets(char *s, int n, FILE *stream) {
  276.     GrafPort *savePort;
  277.     char c, *t = s;
  278.  
  279.     GetPort(&savePort);
  280.     SetPort(theWindow);
  281.  
  282.     for (n -= 1; (n > 0) && ((c = waitChar()) != '\r'); n -= 1) {
  283.         DrawChar(*t++ = c);
  284.         if (screenLength[endLine] < screenWidth)
  285.             screenLine[endLine][screenLength[endLine]++] = c;
  286.     }
  287.  
  288.     if (c == '\r') screenNewline();
  289.  
  290.     *t = '\0';
  291.  
  292.     SetPort(savePort);
  293.  
  294.     return(s);
  295. }
  296.  
  297. void wfprintf(FILE *stream, char *format, ...) {
  298.     char buffer[bufferSize];
  299.     va_list ap;
  300.  
  301.     va_start(ap, format);
  302.     vsprintf(buffer, format, ap);
  303.     va_end(ap);
  304.  
  305.     screenDisplay(buffer);
  306.  
  307.     return;
  308. }
  309.  
  310. void wprintf(char *format, ...) {
  311.     char buffer[bufferSize];
  312.     va_list ap;
  313.  
  314.     va_start(ap, format);
  315.     vsprintf(buffer, format, ap);
  316.     va_end(ap);
  317.  
  318.     screenDisplay(buffer);
  319.  
  320.     return;
  321. }
  322.